package com.example.myapplication.utils;


import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.myapplication.R;
import com.example.myapplication.model.Word;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class SQLiteHelper {

    private String mDBPath = "/storage/emulated/0/words.db";
    private SQLiteDatabase mDataBase;
    private static SQLiteHelper mInstance;
    private Context mContext;

    private SQLiteHelper(Context ctx) {
        mContext = ctx;
    }

    public static SQLiteHelper getInstance(Context ctx) {
        if(mInstance == null) {
            mInstance = new SQLiteHelper(ctx);
        }
        return mInstance;
    }

    private void openDataBase() {

        if(mDataBase == null || !mDataBase.isOpen()) {
            String dst = mContext.getExternalFilesDir("").getAbsolutePath();
            dst += "/words.db";
            InputStream ins = mContext.getResources().openRawResource(R.raw.words);
            FileOutputStream fos;
            try {
                fos = new FileOutputStream(dst);
                byte[] buf = new byte[ins.available()];
                int length = 0;

                do {
                    if(length>0) {
                        fos.write(buf, 0, length);
                    }
                    length = ins.read(buf);
                } while (length  > 0);
                fos.flush();
                fos.close();
                ins.close();

            } catch (Exception e) {
                e.printStackTrace();
            }

            mDataBase = SQLiteDatabase.openDatabase(dst, null,
                    SQLiteDatabase.OPEN_READONLY);
        }
    }

    @SuppressLint("Range")
    public List<Word> query(String dbName, String start, String end) {
        List<Word> result = new ArrayList<>();
        openDataBase();
        String sql = "SELECT * FROM `" + dbName + "` WHERE id >= "+start+" and id <= "+ end;
        Cursor cursor = mDataBase.rawQuery(sql,null);
        while (cursor.moveToNext()) {
            Word w = new Word();
            w.id = cursor.getInt(cursor.getColumnIndex("id"));
            w.en = cursor.getString(cursor.getColumnIndex("en"));
            w.vt = cursor.getString(cursor.getColumnIndex("vt"));
            w.vi = cursor.getString(cursor.getColumnIndex("vi"));
            w.n = cursor.getString(cursor.getColumnIndex("n"));
            w.adj = cursor.getString (cursor.getColumnIndex("adj"));
            w.adv = cursor.getString(cursor.getColumnIndex("adv"));
            w.conj = cursor.getString(cursor.getColumnIndex("conj"));
            w.prep = cursor.getString(cursor.getColumnIndex("prep"));
            w.pron = cursor.getString(cursor.getColumnIndex("pron"));
            w.art = cursor.getString(cursor.getColumnIndex("art"));
            w.abbr = cursor.getString(cursor.getColumnIndex("abbr"));
            w.other = cursor.getString(cursor.getColumnIndex("other"));
            result.add(w);
        }
        return result;
    }
}
